package com.moxiao.yfjh.module.blind.controller.app.controller.notify;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessagePageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageRespVO;
import com.moxiao.yfjh.module.blind.controller.admin.notifymessage.vo.BlindNotifyMessageUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.notifymessage.BlindNotifyMessageConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.notifymessage.BlindNotifyMessageDO;
import com.moxiao.yfjh.module.blind.service.notifymessage.BlindNotifyMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.error;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/2/20
 **/



@Tag(name = "App - 相亲通知消息")
@RestController
@RequestMapping("/blind/notify-message")
@Validated
public class AppBlindNotifyMessageController {
    @Resource
    private BlindNotifyMessageService notifyMessageService;

    @GetMapping("/getNotifyMessageList")
    @Operation(summary = "获得相亲通知消息 所有类型及第一条数据")
    public CommonResult<List<BlindNotifyMessageRespVO>> getNotifyMessageList() {
        Long loginUserId = getLoginUserId();
        if (Objects.isNull(loginUserId)){
            return error(401, "获取个人信息失败");
        }
        return success(notifyMessageService.getNotifyMessageList(loginUserId));
    }


    @GetMapping("/getNotifyMessageByType")
    @Operation(summary = "获得相亲通知消息 类型下面所有的数据")
    public CommonResult<PageResult<BlindNotifyMessageRespVO>> getNotifyMessageByType(@Valid BlindNotifyMessagePageReqVO pageReqVO) {
        Long loginUserId = getLoginUserId();
        if (Objects.isNull(loginUserId)){
            return error(401, "获取个人信息失败");
        }

        pageReqVO.setUserId(String.valueOf(loginUserId));
        PageResult<BlindNotifyMessageDO> notifyMessagePage = notifyMessageService.getNotifyMessagePage(pageReqVO);
        return success(BlindNotifyMessageConvert.INSTANCE.convertPage(notifyMessagePage));
    }


    @GetMapping("/getNotifyMessage")
    @Operation(summary = "获得相亲通知消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlindNotifyMessageRespVO> getNotifyMessage(@RequestParam("id") Long id) {
        BlindNotifyMessageDO notifyMessage = notifyMessageService.getNotifyMessage(id);
        return success(BlindNotifyMessageConvert.INSTANCE.convert(notifyMessage));
    }


    @PutMapping("/updateNotifyMessage")
    @Operation(summary = "更新相亲通知消息")
    public CommonResult<Boolean> updateNotifyMessage(@Valid @RequestBody BlindNotifyMessageUpdateReqVO updateReqVO) {
        notifyMessageService.updateNotifyMessage(updateReqVO);
        return success(true);
    }

    @PutMapping("/oneKeyClearNotifyMessage")
    @Operation(summary = "一键清除")
    public CommonResult<Boolean> oneKeyClearNotifyMessage() {
        Long loginUserId = getLoginUserId();
        if (Objects.isNull(loginUserId)){
            return error(401, "获取个人信息失败");
        }
        notifyMessageService.oneKeyClearNotifyMessage(loginUserId);
        return success(true);
    }

    @GetMapping("/unreadInformation")
    @Operation(summary = "是否有未读信息")
    public CommonResult<Boolean> unreadInformation() {
        Long loginUserId = getLoginUserId();
        if (Objects.isNull(loginUserId)){
            return error(401, "获取个人信息失败");
        }
        BlindNotifyMessageExportReqVO exportReqVO = new BlindNotifyMessageExportReqVO();
        exportReqVO.setUserId(String.valueOf(loginUserId));
        exportReqVO.setReadStatus(0);
        List<BlindNotifyMessageDO> notifyMessageList = notifyMessageService.getNotifyMessageList(exportReqVO);
        if (CollectionUtils.isNotEmpty(notifyMessageList)){
            return success(Boolean.TRUE);
        }
        return success(Boolean.FALSE);
    }



}
